package data

import (
	"github.com/go-kratos/kratos/v2/log"
	"github.com/google/wire"
	"github.com/redis/go-redis/v9"
	"go-kratos-demo/internal/conf"
	"gorm.io/gorm"
)

// ProviderSet is data providers.
var ProviderSet = wire.NewSet(NewData)

// Data .
type Data struct {
	//Redis.
	Rds *redis.Client
	//Mysql
	DbW *gorm.DB
	DbR *gorm.DB
	//kafka
	//Mongo
}

// NewData .
func NewData(c *conf.Data, logger log.Logger) (*Data, func(), error) {

	dbw, cleanUpMysql, err := NewGormDbW(c, logger)
	if err != nil {
		log.NewHelper(logger).Info("error the data resources")
	}
	dbr, cleanUpMysql, err := NewGormDbR(c, logger)
	if err != nil {
		log.NewHelper(logger).Info("error the data resources")
	}
	rds, cleanUpRedis, _ := NewRedis(c, logger)
	if err != nil {
		log.NewHelper(logger).Info("error the data resources")
	}
	cleanup := func() {
		cleanUpMysql()
		cleanUpRedis()
		log.NewHelper(logger).Info("closing the data resources")
	}
	return &Data{
		DbW: dbw,
		DbR: dbr,
		Rds: rds,
	}, cleanup, nil
}
